package com.example.fangqidian.fragment.index;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.fangqidian.R;
import com.example.fangqidian.fragment.BaseFragment;
import com.example.fangqidian.fragment.index.bookcase.BookcaseFragment1;
import com.example.fangqidian.fragment.index.bookcase.BookcaseFragment2;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期:2019/1/17 on 15:25
 * 描述：书架
 * 作者: zhougan
 */
public class BookcaseFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TextView mLeftText;
    private String[] mTabTitles=new String[]{"书架","关注"};
    private List<Fragment> mViews;
    private ViewPagerAdapter mViewPagerAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_bookcase,container,false);

        initView(view);
        initData();
        bindData();
        return view;
    }


    private void initView(View view){
        mTabLayout=view.findViewById(R.id.tableLayout);
        mViewPager=view.findViewById(R.id.viewPager);
        mLeftText=view.findViewById(R.id.textleft);
    }

    private void initData(){
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        mViews=new ArrayList<>();
        mViews.add(new BookcaseFragment1());
        mViews.add(new BookcaseFragment2());
    }

    private void bindData(){
        initViewPager();
        mTabLayout.setupWithViewPager(mViewPager,true);
    }
    private void initViewPager(){
        mViewPagerAdapter=new ViewPagerAdapter(getActivity().getSupportFragmentManager());
        mViewPagerAdapter.setFragments(mViews);
        mViewPager.setAdapter(mViewPagerAdapter);

    }

    private class ViewPagerAdapter extends FragmentPagerAdapter{
        private List<Fragment> fragments;

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setFragments(List<Fragment> fragments) {
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitles[position];
        }
    }


}
